//
//  BannerView.m
//  wsgz
//
//  Created by huck on 2017/5/5.
//  Copyright © 2017年 BoingPay. All rights reserved.
//

#import "BannerView.h"
#import "BannerViewCell.h"
#import "HomeManager.h"
#import "AppUtils.h"
//#import "HabitDevelopSquareViewController.h"
//#import "UIView+Response.h"
//#import "HabitActivityViewController.h"
#import "XGWebViewController.h"


static NSString *s_banerViewCellIdentifier = @"WsgzBannerViewCellIdentifier";

@interface BannerView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, assign) NSInteger timerCount;

@end

@implementation BannerView

+ (instancetype)bannerViewWithFrameView:(UIView *)view
{
    if (nil == view) {
        return nil;
    }
    BannerView *tabView = [[[NSBundle mainBundle] loadNibNamed:@"BannerView" owner:nil options:nil] firstObject];
    tabView.frame = view.bounds;
    [view addSubview:tabView];
    return tabView;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initSubview];
}

-(void)initSubview
{
    [_collectBanner registerNib:[UINib nibWithNibName:@"BannerViewCell" bundle:nil] forCellWithReuseIdentifier:s_banerViewCellIdentifier];
    [self addTimer];
    [self updateContent];
}

- (void)dealloc
{
    [self removeTimer];
}

- (void)updateContent
{
    [_pageBanner setNumberOfPages:[self getResultList].count];
    [_collectBanner reloadData];
}

//根据是否含有链接选择不同的数据源
-(NSArray *)getResultList
{
    return [[HomeManager sharedInstance] bannerViewList];
}

- (void)showHabitDevelops:(NSString *)habitId habitName:(NSString *)habitName
{

}

#pragma mark --UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.width, self.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark --UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self getResultList].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BannerViewCell  *cell = [_collectBanner dequeueReusableCellWithReuseIdentifier:s_banerViewCellIdentifier forIndexPath:indexPath];
    if (nil == [self getResultList] || indexPath.section >= [self getResultList].count) {
        return cell;
    }
    
    BannerViewData *data = [[self getResultList] objectAtIndex:indexPath.section];
    if (nil == data) {
        return cell;
    }
    [cell setInfo:data];
    return cell;
}

#pragma mark --UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (nil == [self getResultList] || indexPath.section >= [self getResultList].count) {
        return;
    }
    
    BannerViewData *bannerData = [[self getResultList] objectAtIndex:indexPath.section];
    if (nil == bannerData) {
        return;
    }
    NSString *linkurl = bannerData.linkUrl;
    //H5
    XGWebViewController *web = [[XGWebViewController alloc] init];
    web.url = linkurl;
    web.hideRightButton = YES;
    [[AppUtils getNavigationController] pushViewController:web animated:YES];
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint pInView = [self convertPoint:_collectBanner.center toView:_collectBanner];
    NSIndexPath *indexPathNow = [_collectBanner indexPathForItemAtPoint:pInView];
    _pageBanner.currentPage = indexPathNow.section;
}

// auto page
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

#pragma mark 当用户停止的时候调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

#pragma mark 添加定时器
- (void)addTimer
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextpage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    _timer = timer;
}

#pragma mark 删除定时器
- (void)removeTimer
{
    if (nil != _timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)nextpage
{
    NSInteger count = [[self getResultList] count];
    if (count <= 0) {
        return;
    }
    
    _timerCount++;
    _timerCount = _timerCount % count;
    
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:0 inSection:_timerCount];
    [_collectBanner scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    _pageBanner.currentPage = _timerCount;
}

@end
